import Mock from 'mockjs'

// 完整的菜单数据
const menuList = [
    {
        id: 'd815a5167ad84c2aab29aa9ab080ca7c',
        parentId: null,
        parentName: null,
        name: 'async_router.system_management',
        title: 'async_router.system_management',
        path: '/system',
        component: 'layout',
        icon: 'ep:setting',
        perms: 'system',
        type: 0, // 0-目录 1-菜单 2-按钮
        menuType: 'M', // 新增：M=目录，C=菜单，B=按钮（与 SQL 的 menu_type 对应）
        status: 1,
        seqNo: 0,
        hidden: false,
        alwaysShow: false,
        redirect: '/system/user',
        noCache: false,
        affix: false,
        breadcrumb: true,
        createTime: '2024-03-07 20:15:00',
        updateTime: '2025-01-01 13:16:54'
    },
    {
        id: '0fdf3ff4c5b348deb960e989e6ee83af',
        parentId: 'd815a5167ad84c2aab29aa9ab080ca7c',
        parentName: '系统管理',
        name: 'async_router.user_management',
        title: 'async_router.user_management',
        path: 'user',
        component: 'views/system/user',
        icon: 'ep:user',
        perms: 'system:user:list',
        type: 1,
        menuType: 'C', // 新增：菜单
        status: 1,
        seqNo: 2,
        hidden: false,
        alwaysShow: false,
        noCache: false,
        affix: false,
        breadcrumb: true,
        createTime: '2024-03-09 13:15:19',
        updateTime: '2024-03-09 13:15:19'
    },
    // ... 其他菜单项（按钮类型示例）
    {
        id: 'a519fd3031a6445d91a8ac06d7a169ea',
        parentId: '0fdf3ff4c5b348deb960e989e6ee83af',
        parentName: '用户管理',
        name: '新增用户',
        title: '新增用户',
        path: null,
        component: null,
        icon: null,
        perms: 'system:user:add',
        type: 2,
        menuType: 'B', // 新增：按钮
        status: 1,
        seqNo: 1,
        hidden: true,
        alwaysShow: null,
        noCache: null,
        affix: null,
        breadcrumb: null,
        createTime: '2025-04-08 20:50:19',
        updateTime: '2025-04-08 20:50:19'
    }
    // ... 其他按钮和菜单项
]

export default [
    // 获取路由
    {
        url: '/dev-api/system/block/menu/getRouters',
        method: 'get',
        response: () => {
            const buildTree = (parentId = null) => {
                return menuList
                    .filter((item) => item.parentId === parentId && item.menuType === 'M')
                    .map((item) => ({
                        path: item.path,
                        name: item.name,
                        hidden: item.hidden,
                        alwaysShow: item.alwaysShow,
                        redirect: item.redirect || null,
                        component: item.component,
                        id: item.id,
                        parentId: item.parentId,
                        meta: {
                            title: item.title,
                            icon: item.icon,
                            perms: item.perms,
                            noCache: item.noCache,
                            affix: item.affix,
                            breadcrumb: item.breadcrumb,
                            activeMenu: null
                        },
                        children: buildTree(item.id)
                    }))
            }

            return {
                code: 1,
                message: 'success',
                data: buildTree()
            }
        }
    },

    // 查询菜单列表
    {
        url: '/dev-api/system/block/menu/list',
        method: 'get',
        response: (config) => {
            const { name, title, status, type } = config.query // 直接获取参数

            // 过滤条件
            let filteredList = [...menuList]

            if (name) {
                filteredList = filteredList.filter((item) =>
                    item.name.toLowerCase().includes(name.toLowerCase())
                )
            }

            if (title) {
                filteredList = filteredList.filter((item) =>
                    item.title.toLowerCase().includes(title.toLowerCase())
                )
            }

            if (status !== undefined && status !== '') {
                filteredList = filteredList.filter((item) => item.status === Number(status))
            }

            if (type !== undefined && type !== '') {
                filteredList = filteredList.filter((item) => item.type === Number(type))
            }

            return {
                code: 1,
                message: 'success',
                data: filteredList
            }
        }
    },

    // 获取菜单树
    {
        url: '/dev-api/system/block/menu/getMenuTree',
        method: 'get',
        response: () => {
            const buildTree = (parentId = null) => {
                return menuList
                    .filter((item) => item.parentId === parentId)
                    .map((item) => ({
                        id: item.id,
                        parentId: item.parentId,
                        name: item.name,
                        title: item.title,
                        type: item.type,
                        status: item.status,
                        seqNo: item.seqNo,
                        children: buildTree(item.id)
                    }))
            }

            return {
                code: 1,
                message: 'success',
                data: buildTree()
            }
        }
    },

    // 新增菜单
    {
        url: '/dev-api/system/block/menu/addMenu',
        method: 'post',
        response: (config) => {
            const newMenu = config.body
            newMenu.id = Mock.mock('@guid')
            newMenu.createTime = new Date().toISOString()
            newMenu.updateTime = new Date().toISOString()

            menuList.push(newMenu)

            return {
                code: 200,
                message: '添加成功',
                data: newMenu.id
            }
        }
    },

    // 编辑菜单
    {
        url: '/dev-api/system/block/menu/editMenu',
        method: 'put',
        response: (config) => {
            const updatedMenu = config.body
            const index = menuList.findIndex((item) => item.id === updatedMenu.id)

            if (index === -1) {
                return {
                    code: 404,
                    message: '菜单不存在'
                }
            }

            updatedMenu.updateTime = new Date().toISOString()
            menuList[index] = {
                ...menuList[index],
                ...updatedMenu
            }

            return {
                code: 1,
                message: '更新成功'
            }
        }
    },

    // 编辑菜单状态（直接获取参数）
    {
        url: '/dev-api/system/block/menu/editMenuStatus',
        method: 'put',
        response: (config) => {
            const { id, status } = config.body
            const index = menuList.findIndex((item) => item.id === id)

            if (index === -1) {
                return {
                    code: 404,
                    message: '菜单不存在'
                }
            }

            menuList[index].status = status
            menuList[index].updateTime = new Date().toISOString()

            return {
                code: 1,
                message: '状态更新成功'
            }
        }
    },

    // 删除菜单
    {
        url: '/dev-api/system/block/menu/deleteMenu/:menuId',
        method: 'delete',
        response: (config) => {
            const { menuId } = config.query
            const index = menuList.findIndex((item) => item.id === menuId)

            if (index === -1) {
                return {
                    code: 404,
                    message: '菜单不存在'
                }
            }

            // 检查是否有子菜单
            const hasChildren = menuList.some((item) => item.parentId === menuId)
            if (hasChildren) {
                return {
                    code: 400,
                    message: '请先删除子菜单'
                }
            }

            menuList.splice(index, 1)

            return {
                code: 1,
                message: '删除成功'
            }
        }
    }
]
